package com.xziying.appstore.plugIn.example;

import com.xziying.appstore.api.ApiInteractive;
import com.xziying.appstore.plugIn.ProtocolEntry;
import com.xziying.appstore.plugIn.cloud.PluginConfigCloud;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

/**
 * CollectionAbstract
 *
 * @author : xziying
 * @create : 2021-03-20 15:26
 */
public abstract class CollectionAbstract implements ProtocolEntry {

    @Override
    public byte[] getConfigHTML(String url) {
        InputStream path = this.getClass().getResourceAsStream("/html/" + url);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            int c;
            byte[] buffer = new byte[1024*4];
            while((c=path.read())!=-1){
                os.write(c);
            }
            while (-1 != (c = path.read(buffer))) {
                os.write(buffer, 0, c);
            }
            return os.toByteArray();
        } catch (Exception e){
            String error = "404";
            char[] chars = error.toCharArray();
            Charset cs = Charset.forName("UTF-8");
            CharBuffer cb = CharBuffer.allocate(chars.length);
            cb.put(chars);
            cb.flip();
            ByteBuffer bb = cs.encode(cb);
            return bb.array();
        }
    }
}
